package com.senior.sendmessage.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alanpoi.analysis.common.utils.ExcelExportUtil;
import com.senior.sendmessage.dto.response.MessageLogExportDTO;
import com.senior.sendmessage.entity.MessageLogPO;
import com.senior.sendmessage.service.MessageLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author baijiliang
 * @date 2022年06月26日 17:49
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class MessageController {
    private final MessageLogService messageLogService;

    @GetMapping("/getMessageLogExport")
    public List<MessageLogExportDTO> getMessageLogExport(final HttpServletRequest request, final HttpServletResponse response) {
        final List<MessageLogPO> list = messageLogService.list();
        final List<MessageLogExportDTO> sorted = list.parallelStream().map(x -> BeanUtil.copyProperties(x, MessageLogExportDTO.class)).collect(Collectors.toList());
//                .stream().sorted(Comparator.comparing(MessageLogExportDTO::getId, Comparator.naturalOrder())).collect(Collectors.toList());
        ExcelExportUtil.export(sorted, MessageLogExportDTO.class, request, response, "我的表格.xlsx");
        return sorted;
    }
}
